import { Injectable } from '@angular/core';
import { PdfDialogComponent } from '@shared/components/pdf-dialog/pdf-dialog.component';
import { NzModalService } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';


interface IOptions {
    title?: string;         // 标题名称
    component?: any;        // 组件名称
    content?: string;       // 展示的信息
    params?: object;        // 组件时需要传入的参数，对面需要用@Input接受
    style?: object;         // 悬浮层样式
    bodyStyle?: any;        // Modal body样式
    okText?: string;        // 确定按钮样式
    cancelText?: string;    // 取消按钮样式
    callback?: any;         // 点击确定后回调函数
    cancelCallback?: any;   // 点击取消后回调函数
    footer?: any;           // 底部内容，设置为null时不展示底部
}

@Injectable()
export class DialogService {
    constructor(
        private modal: NzModalService,
        private message: NzMessageService
    ) { }

    // 打开component的弹窗
    createComponentModal(options: IOptions): void {
        options = options || {};
        this.modal.create({
            nzTitle: options.title || '',
            nzContent: options.component || '',
            // nzViewContainerRef: options.viewContainerRef,
            nzComponentParams: options.params || {},
            nzStyle: options.style || {},
            nzBodyStyle: options.bodyStyle || {},
            nzFooter: options.footer,
            nzOkText: options.okText || '确定',
            nzCancelText: options.cancelText || '取消',
            nzMaskClosable: false,
            nzOnOk: (e) => {
                // tslint:disable-next-line: no-unused-expression
                options.cancelText && options.callback(e);
            }
        });
    }

    // pdf弹窗
    pdfShow(pdfUrl: string): void {
        this.createComponentModal({
            component: PdfDialogComponent,
            style: { top: '0px', width: '920px' },
            bodyStyle: { height: 'calc(100vh-25px)', overflow: 'hidden' },
            footer: null,
            params: { pdfUrl }
        });
    }

    // 确认取消弹窗
    caonfirmShow(options: IOptions): void {
        options = options || {};
        this.modal.confirm({
            nzTitle: options.title || '',
            nzContent: options.content || '',
            nzOkText: options.okText || '确定',
            nzCancelText: options.cancelText || '取消',
            nzOnOk: () => {
                // tslint:disable-next-line: no-unused-expression
                options.callback && options.callback();
            }
        });
    }

    // 添加promise的确认取消弹窗
    promiseConfirmShow(options: IOptions): Promise<boolean> {
        options = options || {};
        return new Promise(resolve => {
            this.modal.confirm({
                nzTitle: options.title || '',
                nzContent: options.content || '',
                nzOkText: options.okText || '确定',
                nzCancelText: options.cancelText || '取消',
                nzOnOk: () => {
                    // tslint:disable-next-line: no-unused-expression
                    options.callback && options.callback(() => resolve(true));
                },
                nzOnCancel: () => {
                    // tslint:disable-next-line: no-unused-expression
                    options.cancelCallback && options.cancelCallback();
                    resolve(true);
                }
            });
        });
    }

    // 消息弹窗
    messageShow(options): void {
        options = options || {};
        this.message.create(options.type || 'info', options.msg || '');
    }
}

